/*
Copyright (c) 2007, EffectiveUI, Inc.

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
    * Neither the name of the EffectiveUI nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@ignore
*/
package com.effectiveui.frameworks.commando.controller {
	
	import com.adobe.cairngorm.control.FrontController;
	import com.effectiveui.frameworks.commando.CommandoInterpereter;
	import com.effectiveui.frameworks.commando.Options;
	
	
	/**
	 * <p>
	 * CommandoController extends Cairngorm's FrontController 
	 * by allowing you to add commands that can be interpereted
	 * from a command line. These commands may have options.
	 * </p>
	 * <p>
	 * Consider this example:
	 * 
	 * <pre>
	 *	public static const ALERT_COMMAND:String = 'alert';
	 *
	 *  public function FooController() {
	 *	
	 *	   var opts:Options = new Options();
	 *	   opts.addOption(new Option('t', 'text', true));
	 *	
	 *	   addCommandWithOptions(ALERT_COMMAND, AlertCommand, opts);
	 * }
	 * </pre>
	 * </p>
	 * 
	 * <p>
	 * In this case any string beginning with 'alert', once parsed by 
	 * the CommandoInterpereter, would be matched to the AlertCommand.
	 * </p>
	 * 
	 * <p>
	 * Notice also that the 'alert' command has an option, 't' or 
	 * long form: 'text'. This would allow a command line in the following forms:
	 * <ul>
	 * <li>alert -t Hello</li>
	 * <li>alert -t "Hello World"</li>
	 * <li>alert -text Hello</li>
	 * <li>alert --text Hello</li>
	 * <li>alert --text "Hello World"</li>
	 * </ul>
	 * </p>
	 * 
	 */	
	public class CommandoController extends FrontController {
		
		/**
		 * Overrides FrontController's addCommand to provide Commando functionality
		 * 
		 * @param commandName the name of the command (must be unique)
		 * @param commandRef Class of command class
		 * @param useWeakReference
		 * 
		 */		
		override public function addCommand(commandName:String, commandRef:Class, useWeakReference:Boolean=true):void {
			super.addCommand(commandName, commandRef, useWeakReference);
			CommandoInterpereter.getInstance().unregisterVerb(commandName);
			CommandoInterpereter.getInstance().registerVerb(commandName);
		}
		
		/**
		 * Overrides FrontController's removeCommand to provide Commando functionality
		 * 
		 * @param commandName command name to remove
		 * 
		 */		
		override public function removeCommand(commandName:String):void {
			super.removeCommand(commandName);
			CommandoInterpereter.getInstance().unregisterVerb(commandName);
		}
		
		/**
		 * Adds a command that takes options.
		 * 
		 * @param commandName command name to add (must be unique)
		 * @param commandRef Class of command
		 * @param options Options for command
		 * @param useWeakReference
		 * 
		 * @see com.effectiveui.frameworks.commando.Options
		 * 
		 */		
		public function addCommandWithOptions(commandName:String, commandRef:Class, options:Options, useWeakReference:Boolean=true):void {
			super.addCommand(commandName, commandRef, useWeakReference);
			CommandoInterpereter.getInstance().unregisterVerb(commandName);
			CommandoInterpereter.getInstance().registerVerb(commandName, options);
		}
		
	}
}